define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope,$rootScope, T_address_seller, GoPage, GoDialog, $timeout) {
		//页面激活
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data(); //必须在定义之后
		}, false);
		$scope.init_data = function() {
			$scope.firstHide = false;//避免闪现“没有数据”
			T_address_seller.address_list(1, '', function(data) {
				if(data != null){
					$scope.datas = data;
				}else{
					$scope.datas = new Object();
				}
				$scope.nPage = 1;
				$scope.firstHide = true;
			})
		}
		
		//添加/编辑
		$scope.onshow_addressadd_seller = function(id) {
			GoPage.go_states("t_address_add_seller", { "id": id });
		}
		//删除
		$scope.delete_address = function(id) {
			GoDialog.confirm("删除","确定要删除吗？",function(){
				T_address_seller.address_delete(id, function(data) {
					if(data.success) {
						GoDialog.toast('删除成功！');
						$scope.init_data();
					} else {
						GoDialog.alert("提示",data.msg);
					}
				})
			})
		}
		//设置默认
		$scope.default_address = function(id, kind) {
			T_address_seller.address_default(id, kind, function(data) {
				if(data.success) {
					GoDialog.toast('设置成功！');
					$scope.init_data();
				} else {
					GoDialog.alert("提示",data.msg);
				}
			})
		}

		/*****************下拉刷新********************/
		$scope.isLoad = false; //可以加载
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			T_address_seller.address_list(1, '', function(data) {
				if(data != null) {
					$scope.datas = data;
				} else {
					$scope.datas = new Object();
				}
				$scope.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			T_address_seller.address_list($scope.nPage + 1, '' ,function(data) {
					if(data != null) {
						if($scope.datas.addr != undefined) {
							$scope.datas.addr = $scope.datas.addr.concat(data.addr);
							$scope.nPage += 1;
						} else {
							$scope.datas = data;
							$scope.nPage = 1;
						}
					}
				},function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};
		/*------------刷新-------------*/
		//注册了全局广播
		$rootScope.$on('editAddressSellerEvent', function(event, data) {
			$scope.init_data();
		});
	}

	controller.$inject = ['$scope','$rootScope', 'T_address_seller', 'GoPage', 'GoDialog', '$timeout'];

	return app.registerController("t_address_sellerctrl",controller);
});